//
//  YXJCDetailVC.m
//  Tunnel
//
//  Created by wbb on 2025/6/14.
//

#import "YJXYDetailVC.h"
#import "InfoView.h"
#import "PlanView.h"
#import "ProgressView.h"
#import "TZImagePickerController.h"

@interface YJXYDetailVC ()
@property (nonatomic, strong) UIImageView * lineImgView;
@property (nonatomic, assign) NSInteger  currentTag;
@property (nonatomic, strong) UIView * infoView;
@property (nonatomic, strong) UIView * planView;
@property (nonatomic, strong) UIView * progressView;
@property (nonatomic,strong) YJXYModel * sjModel;
@property (nonatomic,strong) YJYAModel * yaModel;
@property (nonatomic,copy) NSArray * lcArr;
@property (nonatomic,copy) NSArray * filterArr;
@end

@implementation YJXYDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"应急事件详情";
    
//    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"add_icon"] style:UIBarButtonItemStylePlain target:self action:@selector(add)];
//    self.navigationItem.rightBarButtonItem = right;
    kWEAK_SELF;
    [TYNetWorkTool getRequest:[NSString stringWithFormat:@"%@%@",TYGetEmergencyEventDetail,self.ID] parameters:@{} successBlock:^(BOOL success, NSDictionary * _Nonnull data, NSString * _Nonnull msg) {
        if(success){
            weakSelf.sjModel = [YJXYModel yy_modelWithJSON:data[@"data"]];
            [weakSelf requestOtherApi];
        }
    } failureBlock:^(NSString * _Nonnull description) {

    }];
    


    
}
-(void)requestOtherApi {
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_enter(group);
    kWEAK_SELF;
    [TYNetWorkTool getRequest:[NSString stringWithFormat:@"%@%ld",TYGetEmergencyEventPlanDetail,(long)self.sjModel.planId]  parameters:@{} successBlock:^(BOOL success, NSDictionary * _Nonnull data, NSString * _Nonnull msg) {
        if(success){
            weakSelf.yaModel = [YJYAModel yy_modelWithJSON:data[@"data"]];
        }
        dispatch_group_leave(group);

    } failureBlock:^(NSString * _Nonnull description) {
        dispatch_group_leave(group);

    }];
    dispatch_group_enter(group);
    [TYNetWorkTool postRequest:TYGetEmergencyEventQueryWorkFlow
                    parameters:@{@"eventId":self.sjModel.ID,@"planId":@(self.sjModel.planId)}
                  successBlock:^(BOOL success, NSDictionary * _Nonnull data, NSString * _Nonnull msg) {
        if(success){
           weakSelf.lcArr = [NSArray yy_modelArrayWithClass:[YJLCModel class] json:data[@"data"]];
        }
        dispatch_group_leave(group);
    } failureBlock:^(NSString * _Nonnull description) {
        dispatch_group_leave(group);

    }];
    NSArray * keyArr = @[@"contingencyevent_status",@"contingencyevent_type",@"contingencyevent_level",@"event_sjly",@"egsupplies_type",@"egequipments_type"];
    dispatch_group_enter(group);

    [TYNetWorkTool postRequest:TYGetDictQuery parameters:@{
        @"types":keyArr
    } successBlock:^(BOOL success, id  _Nonnull data, NSString * _Nonnull msg) {
        if(success){
            NSMutableArray * array = [NSMutableArray array];
            for (NSString * key in keyArr) {
                NSMutableArray *arr = [NSMutableArray array];
                if([key isEqualToString:@"contingencyevent_status"]){
                    [arr addObject:@{@"dictLabel":@"事件状态",@"dictValue":@""}];
                }
                if([key isEqualToString:@"contingencyevent_type"]){
                    [arr addObject:@{@"dictLabel":@"事件类型",@"dictValue":@""}];
                }
                if([key isEqualToString:@"contingencyevent_level"]){
                    [arr addObject:@{@"dictLabel":@"事件等级",@"dictValue":@""}];
                }
                for (NSDictionary * dict in  data[@"data"]) {
                    if([dict[@"dictType"] isEqualToString:key]){
                        [arr addObject:dict];
                    }
                }
                [array addObject:arr];
            }
            weakSelf.filterArr = array.copy;
            
        }
        dispatch_group_leave(group);

    } failureBlock:^(NSString * _Nonnull description) {
        dispatch_group_leave(group);

    }];
    
    
    // 所有任务完成后的回调（在主线程执行，因为要更新UI）
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        NSLog(@"-------ui-----------");
        [self initUI];
    });
}
- (void)add {
    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:9 delegate:nil];
    imagePickerVc.allowPickingVideo = NO;
    imagePickerVc.allowEditVideo = NO;
    imagePickerVc.allowPickingImage = YES;
    imagePickerVc.allowPickingOriginalPhoto = NO;
    imagePickerVc.presetName = AVAssetExportPresetHighestQuality;
    // 你可以通过block或者代理，来得到用户选择的照片.
    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {

    }];
    [self presentViewController:imagePickerVc animated:YES completion:nil];
}
- (void)initUI {
    
    UIButton *tabBtn1 = [UIButton buttonWithTitle:@"基础信息" titleColor:main_text_color font:[UIFont systemFontOfSize:14]  target:self action:@selector(onType:)];
    tabBtn1.tag = 10;
    [self.view addSubview:tabBtn1];
    [tabBtn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(0);
        make.width.equalTo(self.view.mas_width).multipliedBy(0.33);
        make.height.offset(30);
    }];
    
    UIButton *tabBtn2 = [UIButton buttonWithTitle:@"应急预案" titleColor:main_text_color font:[UIFont systemFontOfSize:14]  target:self action:@selector(onType:)];
    tabBtn2.tag = 11;
    [self.view addSubview:tabBtn2];
    [tabBtn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(tabBtn1.mas_right);
        make.centerY.equalTo(tabBtn1.mas_centerY);
        make.width.equalTo(tabBtn1.mas_width);
    }];
    
    UIButton *tabBtn3 = [UIButton buttonWithTitle:@"处置进度" titleColor:main_text_color font:[UIFont systemFontOfSize:14]  target:self action:@selector(onType:)];
    tabBtn3.tag = 12;
    [self.view addSubview:tabBtn3];
    [tabBtn3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(tabBtn2.mas_right);
        make.centerY.equalTo(tabBtn1.mas_centerY);
        make.width.equalTo(tabBtn1.mas_width);
        make.right.offset(0);
    }];
    
    UIImageView *lineImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gjtj_tab_icon"]];
    [self.view addSubview:lineImgView];
    self.lineImgView = lineImgView;
    [lineImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(tabBtn1.mas_bottom).offset(0);
        make.centerX.equalTo(tabBtn1.mas_centerX);
        make.width.offset(20);
        make.height.offset(4);
    }];
    
    
    
    InfoView *infoView = [[InfoView alloc] initWithSjModel:self.sjModel filterArr:self.filterArr yaModel:self.yaModel];
    [self.view addSubview:infoView];
    self.infoView = infoView;
    [infoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(44);
        make.left.right.offset(0);
        make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
    }];
    
    PlanView *planView = [[PlanView alloc] initWithLcArr:self.lcArr filterArr:self.filterArr sjModel:self.sjModel];
    planView.hidden = YES;
    [self.view addSubview:planView];
    self.planView = planView;
    [planView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(44);
        make.left.right.offset(0);
        make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
    }];
    
    ProgressView *progressView = [[ProgressView alloc] initWithYJModel:self.sjModel];
    progressView.hidden = YES;
    [self.view addSubview:progressView];
    self.progressView =  progressView;
    [progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(44);
        make.left.right.offset(0);
        make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
    }];
    
}

- (void)onType:(UIButton *)btn {
    NSInteger tag = btn.tag;
    
    
    if(self.currentTag == tag) {
        return;
    }
    
    self.infoView.hidden = tag != 10;
    self.planView.hidden = tag != 11;
    self.progressView.hidden = tag != 12;
    
    if(tag == 10){// select1
        
    }else if (tag == 11){ // select2
        
    }else if (tag == 12){// tab1
        
        
    }
    [self.lineImgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(btn.mas_bottom).offset(0);
        make.centerX.equalTo(btn.mas_centerX);
        make.width.offset(20);
        make.height.offset(4);
    }];
    self.currentTag = tag;
}
@end
